package com.orange.lswidget.widgets.event.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;

import com.orange.lswidget.R;
import com.orange.lswidget.widgets.event.dao.VoiceMailDao;
import com.orange.notificationsummary.event.pil.defaultimpl.BaseEvent;
import com.orange.notificationsummary.event.pil.defaultimpl.VoicemailEvent;
import com.orange.notificationsummary.utils.Constants.EventType;
import com.orange.notificationsummary.utils.EventLSWidgetUtils;
import com.orange.util.LSEventUtils;
import com.orange.util.LogHelper;
import com.orange.util.VoiceMailUtils;

public class VoiceMailManager extends EventManager implements EventType {
	private static final String			TAG		= "VoiceMailManager";

	private static VoiceMailManager		sVoiceMailManager;
	private static Object				sObject	= new Object();

	private VoiceMailDao				mVoiceMailDao;
	private List<Map<String, String>>	mResultList;

	private VoiceMailManager(Context context) {
		super(context);
		mVoiceMailDao = new VoiceMailDao(context);
		mResultList = new ArrayList<Map<String, String>>();

		mBasicIconResId = R.drawable.event_voicemail_icon;
		mDetailedIconResId = R.drawable.event_voicemail_icon_small;
		mBasicTypeString = context.getResources().getString(
				R.string.voice_mail_event_name);
		mDetailedTypeString = context.getResources().getString(
				R.string.voice_mail_event_name);

		mType = VVM_UPDATE;
	}

	public static VoiceMailManager getInstance(Context context) {
		if (sVoiceMailManager == null) {
			synchronized (sObject) {
				if (sVoiceMailManager == null) {
					sVoiceMailManager = new VoiceMailManager(context);
				}
			}
		}
		return sVoiceMailManager;
	}

	public List<Map<String, String>> queryAllVoiceMailToList() {
		if (!mResultList.isEmpty()) {
			mResultList.clear();
		}
		mResultList = mVoiceMailDao.queryAllVoiceMailToList();
		setCount();
		return mResultList;
	}

	@Override
	protected void setCount() {
		mCount = mResultList.size();
	}

	@Override
	public RemoteViews getDetailedView(RemoteViews remoteViews) {
		remoteViews.setViewVisibility(R.id.basic, View.GONE);
		remoteViews.setViewVisibility(R.id.detailed, View.VISIBLE);

		remoteViews.setImageViewResource(R.id.event_detailed_icon,
				mDetailedIconResId);
		remoteViews.setTextViewText(R.id.event_detailed_type,
				mDetailedTypeString);
		remoteViews.setTextViewText(R.id.event_detailed_time, mTime);
		remoteViews.setTextViewText(R.id.event_detailed_number, mFrom);

		remoteViews.setViewVisibility(R.id.mms_attachement_icon, View.GONE);

		remoteViews.setViewVisibility(R.id.mms_image_framelayout, View.GONE);
		remoteViews.setViewVisibility(R.id.contact_image_framelayout,
				View.VISIBLE);

		// image
		if (null == mBitmap) {
			remoteViews.setImageViewResource(R.id.contact_detailed_image,
					R.drawable.event_nobody);
		} else {
			remoteViews
					.setImageViewBitmap(R.id.contact_detailed_image, mBitmap);
		}

		if (mIsVip) {
			remoteViews.setViewVisibility(R.id.contact_vip_image, View.VISIBLE);
		} else {
			remoteViews.setViewVisibility(R.id.contact_vip_image,
					View.INVISIBLE);
		}

		return remoteViews;
	}

	/**
	 * Call this method indicates only one voicemail has been received.
	 */
	@Override
	public void updateDetailedInfo() {
		VoicemailEvent vmInfo = queryVoiceMailEvent();
		if (vmInfo == null) {
			LogHelper.w(TAG, "updateDetailedInfo. vmInfo is null!");
			return;
		}
		mBitmap = vmInfo.contacts_image;
		// mID = String.valueOf(vmInfo.id);

		mLastEventTime = vmInfo.event_date;
		mTime = EventLSWidgetUtils.dateFormat(mContext, mLastEventTime);

		mIsVip = vmInfo.isVip;
		if (!EventLSWidgetUtils.getEventPreviewSetting(mContext)) {
			mNumberLabel = vmInfo.evnet_number_type;
		}
		mNumberCountByContact = vmInfo.phoneNumberCount;

		List<String> numberList = VoiceMailUtils.getNumbersByResult(mContext,
				mResultList);
		LSEventUtils.updateNamesByNumbers(mContext, numberList, mNames);
		if (mNumberCountByContact > 1 && !TextUtils.isEmpty(mNumberLabel)) {
			mFrom = LSEventUtils.getNameString(mContext, mNames, mNumberLabel);
		} else {
			mFrom = LSEventUtils.getNameString(mContext, mNames);
		}

		LogHelper.i(TAG, "updateDetailedInfo. mTime: " + mTime + " mFrom: "
				+ mFrom + " ,mIsVip: " + mIsVip + " ,mNumberCountByContact: "
				+ mNumberCountByContact + " ,mNumberLabel: " + mNumberLabel);
	}

	@Override
	public void updateSummaryInfo() {
		if (null == mResultList || mResultList.isEmpty()) {
			mLastEventTime = 0;
		} else {
			LogHelper.i(TAG, "updateSummaryInfo. mResultList.size(): "
					+ mResultList.size());
			if (mCount > 1) {
				mBasicTypeString = mContext.getResources().getString(
						R.string.voice_mails);
			} else {
				mBasicTypeString = mContext.getResources().getString(
						R.string.voice_mail_event_name);
				mDetailedTypeString = mContext.getResources().getString(
						R.string.voice_mail_event_name);
			}

			// sort all kinds of event by time
			mLastEventTime = Long.parseLong(mResultList.get(0).get("date"));
			mTime = EventLSWidgetUtils.dateFormat(mContext, mLastEventTime);
			LogHelper.i(TAG, "updateSummaryInfo. mLastEventTime: "
					+ mLastEventTime);

			List<String> numberList = VoiceMailUtils.getNumbersByResult(
					mContext, mResultList);
			LSEventUtils.updateNamesByNumbers(mContext, numberList, mNames);
		}
	}

	public VoicemailEvent queryVoiceMailEvent() {
		List<BaseEvent> vvmList = mVoiceMailDao.queryVoiceMailList();
		VoicemailEvent vmInfo = null;

		if (vvmList != null && !vvmList.isEmpty()) {
			vmInfo = (VoicemailEvent) vvmList.get(0);
		}
		return vmInfo;
	}

}
